﻿using System.Collections.Generic;
using HK.Core.Common.Data;
using HK.Core.Common.Protocol.Data;
using HK.Core.GUIEx.Protocol.Attribute;
using HK.Core.Utils;
using HK.Editor.Common;
using HK.Editor.Common.Protocol;
using HK.Editor.GUIEx.Data;
using HK.Editor.GUIEx.Drawer;
using HK.Editor.GUIEx.Protocol.Drawer;
using HK.Editor.GUIEx.Protocol.Window;
using HK.Editor.Utils;
using UnityEngine;
using HLogger = HK.Core.Logs.Logger;

namespace HK.Editor.GUIEx.Window
{
    
    /// <summary>
    /// 白名单窗体
    /// </summary>
    public class WhiteListWindow : WindowBase<WhiteListWindow, WindowConf, WhiteListWindowData>, IWhiteListWindow<WhiteListWindowData>
    {
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="iWhiteList">白名单</param>
        /// <param name="iSaveCallback">保存回调</param>
        public static void ShowWindow<TWhileList>(TWhileList iWhiteList, SaveWhiteList iSaveCallback)
            where TWhileList : WhiteList, IOBrowse
        {
            var conf = WindowConf.Create("白名单", new Rect(0, 0, 600, 400), (int)GUIOperateOptions.WhiteListWindow);
            
            //创建窗口	
            var window = UtilsWindow.CreateWindow<WhiteListWindow, WindowConf>(conf, false, false);	
            if (null == window) {
                HLogger.Error ("WhiteListWindow::ShowWindow() -> Create Window Failed!!");
                return;
            }
            window.ResetConf(conf);
            window.ResetDataByWhiteList<TWhileList>(iWhiteList, iSaveCallback);
            window.OpenWindow();
        }

        /// <summary>
        /// 总大小
        /// </summary>
        public int TotalSize => Data.List.Count;

        /// <summary>
        /// 检索中
        /// </summary>
        public bool Searching => Pagination?.Searching ?? false;

        /// <summary>
        /// 可见数量
        /// </summary>
        public int VisibleSize => Pagination?.VisibleSize ?? 0;

        /// <summary>
        /// 保存白名单
        /// </summary>
        public SaveWhiteList Save { get; private set; } = null;

#region Window

        /// <summary>
        /// 窗体关闭回调
        /// </summary>
        protected override void OnWindowClosed()
        {
            Save?.Invoke(Data.List);
        }

#endregion

#region Init
        
        /// <summary>
        /// 重置数据
        /// </summary>
        /// <param name="iList">白名单</param>
        /// <param name="iSaveCallback">保存回调</param>
        public void ResetDataByWhiteList<TWhiteList>(TWhiteList iList, SaveWhiteList iSaveCallback)
            where TWhiteList : WhiteList, IOBrowse
        {
            Data.ResetWhiteList(iList);
            
            // 初始化分页信息
            var conf = HK.Editor.GUIEx.Pagination.Pagination.Create(20);
            Pagination = IoPaginationDrawer.Create(Data.List.List, conf, iList,
                Data.List.AddTargets, OnPaginationSearchBtnClicked, false, true,
                (int)GUIOperateOptions.IoWhiteList);
            
            Save = iSaveCallback;
        }

#endregion

#region Event
        
        /// <summary>
        /// 分页面板检索按钮点击事件
        /// </summary>
        /// <param name="iSearchKey">检索Key</param>
        private void OnPaginationSearchBtnClicked(string iSearchKey)
        {
            //ResetCaption();
        }
        
#endregion

#region Draw

        /// <summary>
        /// 分页绘制器
        /// </summary>
        public IPaginationDrawer Pagination { get; private set; } = null;

        /// <summary>
        /// 绘制窗体Body
        /// </summary>
        /// <param name="iRect">制矩形范围</param>
        /// <param name="iEvent">Event</param>
        /// <returns>制矩形范围</returns>
        protected override Rect DrawWinBody(Rect iRect, Event iEvent)
        {
            var displayRect = iRect;

            var bodyBgRect = displayRect;
            bodyBgRect.yMin += VerticalSpacing;
            bodyBgRect.yMax -= VerticalSpacing;
            bodyBgRect = UtilsGUIEx.DrawGenericBg(bodyBgRect, iEvent);

            var paginationRect = bodyBgRect;
            paginationRect.yMin += 1;
            Pagination.Draw(paginationRect, iEvent);
            
            return displayRect;
        }

#endregion

    }
}
